package xiaoqi.springboot.boot.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import xiaoqi.springboot.boot.constants.ServiceExceptionEnum;
import xiaoqi.springboot.boot.core.exception.ServiceException;
import xiaoqi.springboot.boot.core.vo.CommonResult;
import xiaoqi.springboot.boot.vo.UserVO;

import java.util.UUID;

@RestController
@RequestMapping("/users")
public class UserController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @GetMapping("/get")
    public UserVO get(@RequestParam("id") Integer id) {
        return new UserVO().setId(id).setUsername(UUID.randomUUID().toString());
    }

    @GetMapping("/get2")
    public CommonResult<UserVO> get2(@RequestParam("id") Integer id) {
        UserVO user = new UserVO().setId(id).setUsername(UUID.randomUUID().toString());
        return CommonResult.success(user);
    }


    @GetMapping("/exception-01")
    public UserVO exception1() {
        throw new NullPointerException("爆了");
    }

    @GetMapping("/exception-02")
    public UserVO exception2() {
        throw new ServiceException(ServiceExceptionEnum.USER_NOT_FOUND);
    }

    @GetMapping("/exceptiom-03")
    public void exception03() {
        logger.info("[exception03]");
        throw new ServiceException(ServiceExceptionEnum.USER_NOT_FOUND);
    }

    @GetMapping("/do_something")
    public void  doSomething() {
        logger.info("[doSomething]");
    }


    @GetMapping("/current_user")
    public UserVO currentUser() {
        logger.info("[currentUser]");
        return new UserVO().setId(10).setUsername(UUID.randomUUID().toString());
    }


}
